#include "DynamicMessageTest.h"
#include <QFormLayout>
#include <QGroupBox>
#include <QDebug>

DynamicMessageTest::DynamicMessageTest(QWidget *parent) : ElaWidget(parent)
{
    setupUI();
}

void DynamicMessageTest::setupUI()
{
    m_messageBtn = new ElaMessageButton("动态消息", this);

    // 配置控件
    m_titleEdit = new QLineEdit(this);
    m_titleEdit->setText("动态标题");
    m_titleEdit->setPlaceholderText("输入消息标题");

    m_textEdit = new QLineEdit(this);
    m_textEdit->setText("这是动态配置的消息内容");
    m_textEdit->setPlaceholderText("输入消息内容");

    m_typeCombo = new QComboBox(this);
    m_typeCombo->addItem("成功", ElaMessageBarType::Success);
    m_typeCombo->addItem("错误", ElaMessageBarType::Error);
    m_typeCombo->addItem("警告", ElaMessageBarType::Warning);
    m_typeCombo->addItem("信息", ElaMessageBarType::Information);

    m_positionCombo = new QComboBox(this);
    m_positionCombo->addItem("左上角", ElaMessageBarType::TopLeft);
    m_positionCombo->addItem("右上角", ElaMessageBarType::TopRight);
    m_positionCombo->addItem("顶部居中", ElaMessageBarType::Top);
    m_positionCombo->addItem("左下角", ElaMessageBarType::BottomLeft);
    m_positionCombo->addItem("右下角", ElaMessageBarType::BottomRight);
    m_positionCombo->addItem("底部居中", ElaMessageBarType::Bottom);

    m_durationSpin = new QSpinBox(this);
    m_durationSpin->setRange(1000, 10000);
    m_durationSpin->setSingleStep(500);
    m_durationSpin->setValue(3000);
    m_durationSpin->setSuffix(" ms");

    m_radiusSpin = new QSpinBox(this);
    m_radiusSpin->setRange(0, 20);
    m_radiusSpin->setValue(3);

    // 配置组
    QGroupBox *configGroup = new QGroupBox("消息配置", this);
    QFormLayout *configLayout = new QFormLayout(configGroup);
    configLayout->addRow("消息标题:", m_titleEdit);
    configLayout->addRow("消息内容:", m_textEdit);
    configLayout->addRow("消息类型:", m_typeCombo);
    configLayout->addRow("显示位置:", m_positionCombo);
    configLayout->addRow("显示时长:", m_durationSpin);
    configLayout->addRow("按钮圆角:", m_radiusSpin);

    // 主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->addWidget(m_messageBtn);
    mainLayout->addWidget(configGroup);

    // 初始配置
    updateMessageButton();

    // 连接信号
    connect(m_titleEdit, &QLineEdit::textChanged, this, &DynamicMessageTest::onConfigChanged);
    connect(m_textEdit, &QLineEdit::textChanged, this, &DynamicMessageTest::onConfigChanged);
    connect(m_typeCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DynamicMessageTest::onConfigChanged);
    connect(m_positionCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &DynamicMessageTest::onConfigChanged);
    connect(m_durationSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &DynamicMessageTest::onConfigChanged);
    connect(m_radiusSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &DynamicMessageTest::onConfigChanged);
    connect(m_messageBtn, &ElaMessageButton::clicked, this, &DynamicMessageTest::onShowMessage);
}

void DynamicMessageTest::updateMessageButton()
{
    m_messageBtn->setBarTitle(m_titleEdit->text());
    m_messageBtn->setBarText(m_textEdit->text());
    m_messageBtn->setMessageMode(static_cast<ElaMessageBarType::MessageMode>(m_typeCombo->currentData().toInt()));
    m_messageBtn->setPositionPolicy(static_cast<ElaMessageBarType::PositionPolicy>(m_positionCombo->currentData().toInt()));
    m_messageBtn->setDisplayMsec(m_durationSpin->value());
    m_messageBtn->setBorderRadius(m_radiusSpin->value());
}

void DynamicMessageTest::onConfigChanged()
{
    updateMessageButton();
}

void DynamicMessageTest::onShowMessage()
{
    qDebug() << "显示动态配置的消息";
    qDebug() << "标题:" << m_titleEdit->text();
    qDebug() << "内容:" << m_textEdit->text();
    qDebug() << "类型:" << m_typeCombo->currentText();
    qDebug() << "位置:" << m_positionCombo->currentText();
    qDebug() << "时长:" << m_durationSpin->value();
}